﻿
 
g_bool_edit = false;				//是否修改状态
g_bool_edit_query = false;			//修改状态, 是否查询了数据

//-------------页面初始化-----------------------------------------------------
function initPage()
{
    GetListType_1();  //查找一级类
}

//-------------查找分类-----------------------------------------------------
function GetListType_1()
{  
	PostToPHP(__GetListType_1_Callback, "info", "query_type_1", ""); 
}
function __GetListType_1_Callback(text)
{   
	//alert(text);
	var jsonObj = String2JSON(text);
	var ds = GetDataSet(jsonObj);

	if(ds && ds[0])
	{
        FillSelectObj("type_1", ds[0]);  
		JudgeURL();  ////判断url   
		//alert($('type_1').value);
		on_select_change_type1();
	}
}
function on_select_change_type1()
{  
	tmpTypeVal = $('type_1').value;      //当前选择类别
	var data = "&typeName=" + tmpTypeVal; 
	PostToPHP(__on_select_change_type1_callback, "info", "query_type_2", data); 
}
function __on_select_change_type1_callback(text)
{
	//alert(text);
	var jsonObj = String2JSON(text);
	var ds = GetDataSet(jsonObj);
	if(ds && ds[0])
	{
        FillSelectObj("type_2", ds[0]);
		
		if(g_bool_edit)  //编辑状态
		{ 
			$("type_2").value = g_str_type_2; 
			//DelSelectObjExt("type_2", g_str_type_2); 
		}
		
		on_select_change_type2();
	}
}

function on_select_change_type2()
{  
	tmpTypeVal_1 = $('type_1').value;      	//当前选择类别1
	tmpTypeVal = $('type_2').value;      	//当前选择类别2
	var data = "&typeName_1=" + tmpTypeVal_1;
	data += "&typeName=" + tmpTypeVal;
	PostToPHP(__on_select_change_type2_callback, "info", "query_type_3", data); 
}
function __on_select_change_type2_callback(text)
{
	//alert(text);
	var jsonObj = String2JSON(text);
	var ds = GetDataSet(jsonObj);
	if(ds && ds[0])
	{
        FillSelectObj("type_3", ds[0]);
        OnGetData();  //获取某条记录
	}
}

var g_str_type_1 = "";
var g_str_type_2 = "";
var g_str_sex = "0"; 
var g_str_goods_id = "0"; 

//--------判断url 是否存在id, 存在则查询记录， 不存在则表示是新增加---------------------------------------
function JudgeURL()
{
    g_str_type_1 = GetUrlParm("type_1");  //alert(g_str_type_1);
    g_str_type_2 = GetUrlParm("type_2");   
    g_str_sex = GetUrlParm("sex"); 
    g_str_goods_id = GetUrlParm("goods_id"); 
    if(!g_bool_edit && g_str_goods_id && g_str_goods_id.length > 0)
    {   
		g_bool_edit = true;  //修改状态
		
        $("type_1").value = g_str_type_1; 
		DelSelectObjExt("type_1", g_str_type_1); 
		
        //$("goods_id").value = g_str_goods_id;
        //$("sex").value = strSex;
    } 
}

//------获取某条记录---------------------------------------------------------------
function OnGetData()
{  
	if(g_bool_edit && !g_bool_edit_query)  //修改状态, 未获取记录, 则获取记录
	{
		var data = "&goods_id=" + g_str_goods_id;
		data += "&type_1=" + g_str_type_1;
		data += "&type_2=" + g_str_type_2;
		data += "&sex=" + g_str_sex;
		PostToPHP(__OnGetDataCallback, "info", "select_goods_info", data);
	}
}
function __OnGetDataCallback(text)
{
	g_bool_edit_query = true; //修改状态, 查询了数据
	
    //alert(text);   
    var jsonObj = String2JSON(text);
	var ds = GetDataSet(jsonObj);
 	if(jsonObj && ds[0])
    {   
        var items = ds[0];  
        
		for(var name in items)
		{ 
			tmpObj = $(name);   
			if(tmpObj)
			{	
				//alert(tmpObj);
				tmpObj.value = items[name];
			}
		}
 
		bFile_0 = (items["file_0"] == "true");
		bFile_1 = (items["file_1"] == "true");
		bFile_2 = (items["file_2"] == "true");
		bFile_3 = (items["file_3"] == "true");
		bFile_4 = (items["file_4"] == "true");
 
		strPossfix = GetFilePostfix(); 		//文件后缀
        if($('type_2').value == "music")
		{
			strPossfix = "mp3";
		}
		 
		var tempStr = items["filepath"] + items["goods_id"] + "_" + items["sex"] + "_"; 
		
		if(bFile_0) 
			InitFileSapn("span_file_0", "file_0", tempStr+"0."+strPossfix);
		if(bFile_1)
			InitFileSapn("span_file_1", "file_1", tempStr+"1."+strPossfix);
        if(bFile_2) 
            InitFileSapn("span_file_2", "file_2", tempStr+"2."+strPossfix); 
        if(bFile_3) 
            InitFileSapn("span_file_3", "file_3", tempStr+"3."+strPossfix); 
        if(bFile_4) 
            InitFileSapn("span_file_4", "file_4", tempStr+"4."+strPossfix); 
     } 
}
//初始化span
function InitFileSapn(spanID, fileInputID, filePath)
{ 
    DisabledObj(fileInputID, true);  //禁用file控件
    DisplayObj(fileInputID, "none"); //隐藏file控件    
    
    AppendElementText(spanID, "    ");
    AppendElementA(spanID, "OnChangeFile", fileInputID, "修改"); 
    AppendElementText(spanID, "    ");
    
    AppendElementA(spanID, "OnCancel", fileInputID, "取消");
    AppendElementText(spanID, "    ");
    
    var swfPath = GetSwfFileRootPath() + filePath;
    AppendElementA2(spanID, swfPath);
} 
//修改
function OnChangeFile(fileInputID)
{  
    DisplayObj(fileInputID, "");
    DisabledObj(fileInputID, false);
}
//取消
function OnCancel(fileInputID)
{  
    DisabledObj(fileInputID, true);
    DisplayObj(fileInputID, "none");
} 



//----修改或插入提交之前判断-------------------------------------------
function submit_before()
{
	$('form').action = GetSwfFileRootPath() + "main.php"; 
	$('command_type').value = $('type_1').value;
	
    //if($("file_2")) return CheckFilePostfix_ext("file_0", "file_1", "file_2"); 
    //else return CheckFilePostfix_ext("file_0", "file_1");
}